<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfcomponent displayname="Validation" output="false">
		<cfproperty name="ValidationId" type="numeric" default="" />
		<cfproperty name="FieldId" type="numeric" default="" />
		<cfproperty name="ValidationRule" type="string" default="" />
		<cfproperty name="ValidationVal" type="string" default="" />
		<cfproperty name="ValidationMsg" type="string" default="" />
		
	<!---
	PROPERTIES
	--->
	<cfset variables.instance = StructNew() />

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" returntype="Validation" output="false">
		<cfargument name="ValidationId" type="string" required="false" default="" />
		<cfargument name="FieldId" type="string" required="false" default="" />
		<cfargument name="ValidationRule" type="string" required="false" default="" />
		<cfargument name="ValidationVal" type="string" required="false" default="" />
		<cfargument name="ValidationMsg" type="string" required="false" default="" />
		
		<!--- run setters --->
		<cfset setValidationId(arguments.ValidationId) />
		<cfset setFieldId(arguments.FieldId) />
		<cfset setValidationRule(arguments.ValidationRule) />
		<cfset setValidationVal(arguments.ValidationVal) />
		<cfset setValidationMsg(arguments.ValidationMsg) />
		
		<cfreturn this />
 	</cffunction>

	<!---
	PUBLIC FUNCTIONS
	--->
	<cffunction name="setMemento" access="public" returntype="Validation" output="false">
		<cfargument name="memento" type="struct" required="yes"/>
		<cfset variables.instance = arguments.memento />
		<cfreturn this />
	</cffunction>
	<cffunction name="getMemento" access="public" returntype="struct" output="false" >
		<cfreturn variables.instance />
	</cffunction>

	<cffunction name="validate" access="public" returntype="array" output="false">
		<cfset var errors = arrayNew(1) />
		<cfset var thisError = structNew() />
		
		<!--- ValidationId --->
		<cfif (len(trim(getValidationId())) AND NOT isNumeric(trim(getValidationId())))>
			<cfset thisError.field = "ValidationId" />
			<cfset thisError.type = "invalidType" />
			<cfset thisError.message = "ValidationId is not numeric" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		
		<!--- FieldId --->
		<cfif (NOT len(trim(getFieldId())))>
			<cfset thisError.field = "FieldId" />
			<cfset thisError.type = "required" />
			<cfset thisError.message = "FieldId is required" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		<cfif (len(trim(getFieldId())) AND NOT isNumeric(trim(getFieldId())))>
			<cfset thisError.field = "FieldId" />
			<cfset thisError.type = "invalidType" />
			<cfset thisError.message = "FieldId is not numeric" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		
		<!--- ValidateAt --->
		<cfif (NOT len(trim(getValidationRule())))>
			<cfset thisError.field = "ValidationRule" />
			<cfset thisError.type = "required" />
			<cfset thisError.message = "ValidationRule is required" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		<cfif (len(trim(getValidationRule())) AND NOT IsSimpleValue(trim(geValidationRule())))>
			<cfset thisError.field = "ValidationRule" />
			<cfset thisError.type = "invalidType" />
			<cfset thisError.message = "ValidationRule is not a string" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		<cfif (len(trim(getValidationRule())) GT 50)>
			<cfset thisError.field = "ValidationRule" />
			<cfset thisError.type = "tooLong" />
			<cfset thisError.message = "ValidationRule is too long" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		
		<!--- Validation --->
		<cfif (NOT len(trim(getValidationVal())))>
			<cfset thisError.field = "ValidationVal" />
			<cfset thisError.type = "required" />
			<cfset thisError.message = "ValidationVal is required" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		<cfif (len(trim(getValidationVal())) AND NOT IsSimpleValue(trim(getValidationVal())))>
			<cfset thisError.field = "ValidationVal" />
			<cfset thisError.type = "invalidType" />
			<cfset thisError.message = "ValidationVal is not a string" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		<cfif (len(trim(getValidationVal())) GT 500)>
			<cfset thisError.field = "ValidationVal" />
			<cfset thisError.type = "tooLong" />
			<cfset thisError.message = "ValidationVal is too long" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		
		<!--- ValidationMsg --->
		<cfif (len(trim(getValidationMsg())) AND NOT IsSimpleValue(trim(getValidationMsg())))>
			<cfset thisError.field = "ValidationMsg" />
			<cfset thisError.type = "invalidType" />
			<cfset thisError.message = "ValidationMsg is not a string" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		<cfif (len(trim(getValidationMsg())) GT 2000)>
			<cfset thisError.field = "ValidationMsg" />
			<cfset thisError.type = "tooLong" />
			<cfset thisError.message = "ValidationMsg is too long" />
			<cfset arrayAppend(errors,duplicate(thisError)) />
		</cfif>
		
		<cfreturn errors />
	</cffunction>

	<!---
	ACCESSORS
	--->
	<cffunction name="setValidationId" access="public" returntype="void" output="false">
		<cfargument name="ValidationId" type="string" required="true" />
		<cfset variables.instance.ValidationId = arguments.ValidationId />
	</cffunction>
	<cffunction name="getValidationId" access="public" returntype="string" output="false">
		<cfreturn variables.instance.ValidationId />
	</cffunction>

	<cffunction name="setFieldId" access="public" returntype="void" output="false">
		<cfargument name="FieldId" type="string" required="true" />
		<cfset variables.instance.FieldId = arguments.FieldId />
	</cffunction>
	<cffunction name="getFieldId" access="public" returntype="string" output="false">
		<cfreturn variables.instance.FieldId />
	</cffunction>

	<cffunction name="setValidationRule" access="public" returntype="void" output="false">
		<cfargument name="ValidationRule" type="string" required="true" />
		<cfset variables.instance.ValidationRule = arguments.ValidationRule />
	</cffunction>
	<cffunction name="getValidationRule" access="public" returntype="string" output="false">
		<cfreturn variables.instance.ValidationRule />
	</cffunction>

	<cffunction name="setValidationVal" access="public" returntype="void" output="false">
		<cfargument name="ValidationVal" type="string" required="true" />
		<cfset variables.instance.ValidationVal = arguments.ValidationVal />
	</cffunction>
	<cffunction name="getValidationVal" access="public" returntype="string" output="false">
		<cfreturn variables.instance.ValidationVal />
	</cffunction>

	<cffunction name="setValidationMsg" access="public" returntype="void" output="false">
		<cfargument name="ValidationMsg" type="string" required="true" />
		<cfset variables.instance.ValidationMsg = arguments.ValidationMsg />
	</cffunction>
	<cffunction name="getValidationMsg" access="public" returntype="string" output="false">
		<cfreturn variables.instance.ValidationMsg />
	</cffunction>


	<!---
	DUMP
	--->
	<cffunction name="dump" access="public" output="true" return="void">
		<cfargument name="abort" type="boolean" default="false" />
		<cfdump var="#variables.instance#" />
		<cfif arguments.abort>
			<cfabort />
		</cfif>
	</cffunction>

</cfcomponent>


